/**
 * SlideButton.java [V 1.0.0]
 * classes : com.xiaocq.wordsgod.SlideButton
 * xiaocq Create at 2012-7-12 下午4:33:01
 */
package com.xiaocq.wordsgod;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

/**
 * com.xiaocq.wordsgod.SlideButton
 * @author Mystory
 * Create at 2012-7-12 下午4:33:01
 */
public class SlideButton extends View implements OnTouchListener{
	
	private Matrix matrix;
	private Paint paint;
	
	private Bitmap background,frontground;
	private int onToLeft, offToLeft;
	private float currentX;
	
	private boolean isSliding, isSwitchOn;
	
	private boolean hasSlideListener = false;
	
	private OnSlideListener slideListener = null;
	
	public SlideButton(Context context){
		super(context);
	}
	
	public SlideButton(Context context, AttributeSet attrs){
		super(context, attrs);
		init();
	}
	
	public SlideButton(Context context, AttributeSet attrs, int defStyle){
		super(context, attrs, defStyle);
		init();
	}
	
	private void init(){
		setOnTouchListener(this);
		matrix = new Matrix();
		paint = new Paint();
	}
	
	public void setImageResource(int backgroundId, int frontgroundId){
		this.background = BitmapFactory.decodeResource(getResources(), backgroundId);
		this.frontground = BitmapFactory.decodeResource(getResources(), frontgroundId);
		
		onToLeft = background.getWidth() - frontground.getWidth();
		offToLeft = 0;
	}

	public void setSwitchOn(boolean switchState){
		isSwitchOn = switchState;
	}
	
	public boolean isSwitchOn(){
		return isSwitchOn;
	}
	
	public void updateButtonState(boolean switchState){
		setSwitchOn(switchState);
		invalidate();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		float leftOfFrontground = 0;
		
		canvas.drawBitmap(background, matrix, paint);
		
		if(isSliding){
			if(currentX >= background.getWidth()){
				leftOfFrontground = onToLeft;
			}else {
				leftOfFrontground = currentX - frontground.getWidth() / 2;
			}
		}else if(isSwitchOn){
			leftOfFrontground = onToLeft;
		}else {
			leftOfFrontground = offToLeft;
		}
		
		if(leftOfFrontground < 0){
			leftOfFrontground = offToLeft;
		}else if(leftOfFrontground > onToLeft){
			leftOfFrontground = onToLeft;
		}
		
		canvas.drawBitmap(frontground, leftOfFrontground, 0, paint);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(background.getWidth(), background.getHeight());
	}
	
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		
		case MotionEvent.ACTION_MOVE:
			currentX = event.getX();
			break;
		case MotionEvent.ACTION_DOWN:
			if(event.getX() > background.getWidth() || event.getY() > background.getHeight()){
				return false;
			}
			
			isSliding = true;
			currentX = event.getX();
			
			break;
			
		case MotionEvent.ACTION_UP:
			isSliding = false;
			boolean savedSwitchState = isSwitchOn;
			
			if(event.getX() >= background.getWidth() / 2){
				isSwitchOn = true;
			}else{
				isSwitchOn = false;
			}
			
			if(hasSlideListener && (savedSwitchState != isSwitchOn)){
				slideListener.onSlideCompleted(isSwitchOn);
			}
		default:
			break;
		}
		
		invalidate();
		
		return true;
	}
	
	public void setOnSlideListener(OnSlideListener listener){
		slideListener = listener;
		hasSlideListener = true;
	}
	
	public interface OnSlideListener{
		abstract void onSlideCompleted(boolean isSwitchOn);
	}
}
